package com.tengroup.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tengroup.dao.ExampaperDao;
import com.tengroup.dao.StudentScoreDao;
import com.tengroup.entity.ExampaperEntity;
import com.tengroup.entity.StudentScoreEntity;
import com.tengroup.entity.UserInfoEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;


@Service("studentScoreService")
public class StudentScoreServiceImpl extends ServiceImpl<StudentScoreDao, StudentScoreEntity> implements StudentScoreService{
    @Autowired
    private StudentScoreDao studentScoreDao;
    @Autowired
    private ExampaperService exampaperService;
    @Autowired
    private UserService userService;
    @Override
    public Integer addScore(int i){
        StudentScoreEntity studentScoreEntity = new StudentScoreEntity();
        ExampaperEntity exampaperEntity = exampaperService.getById(i);
        studentScoreEntity.setPaperId((long) i);
        studentScoreEntity.setStudentScore(exampaperEntity.getGrade());
        studentScoreEntity.setUserName(exampaperEntity.getName());
        UserInfoEntity userInfoEntity = userService.findUserByUsername(exampaperEntity.getName());
        studentScoreEntity.setUserId(userInfoEntity.getUserId());
        studentScoreEntity.setClassId(Long.valueOf(userInfoEntity.getUserClass()));
        return studentScoreDao.insert(studentScoreEntity);
    }//半手动添加版
    public Integer addScoreByHand(StudentScoreEntity studentScoreEntity){
        return baseMapper.insert(studentScoreEntity);
    }//全手动添加版……
    @Override
    public Integer updateScore(StudentScoreEntity studentScoreEntity) {
        return baseMapper.updateById(studentScoreEntity);
    }
    public Integer deleteScore(Long id){
        return baseMapper.deleteById(id);
    }
    @Override
    public IPage<StudentScoreEntity> QueryUserId(StudentScoreEntity studentScoreEntity, int pageNo, int pageSize) {

        LambdaQueryWrapper<StudentScoreEntity> query = Wrappers.lambdaQuery();
        query.eq(studentScoreEntity.getUserId() != null, StudentScoreEntity::getUserId, studentScoreEntity.getUserId());

        if( pageNo == 0 )
        {
            pageNo = 1;
        }
        if( pageSize == 0 ){
            pageSize = 10;
        }

        IPage<StudentScoreEntity> page = Page.of(pageNo, pageSize);

        return baseMapper.selectPage(page, query);
    }
    @Override
    public IPage<StudentScoreEntity> QueryPaperId(StudentScoreEntity studentScoreEntity, int pageNo, int pageSize) {

        LambdaQueryWrapper<StudentScoreEntity> query = Wrappers.lambdaQuery();
        query.eq(studentScoreEntity.getPaperId() != null, StudentScoreEntity::getPaperId, studentScoreEntity.getPaperId());

        if( pageNo == 0 )
        {
            pageNo = 1;
        }
        if( pageSize == 0 ){
            pageSize = 10;
        }

        IPage<StudentScoreEntity> page = Page.of(pageNo, pageSize);

        return baseMapper.selectPage(page, query);
    }
    @Override
    public IPage<StudentScoreEntity> QueryClassId(StudentScoreEntity studentScoreEntity, int pageNo, int pageSize) {

        LambdaQueryWrapper<StudentScoreEntity> query = Wrappers.lambdaQuery();
        query.eq(studentScoreEntity.getClassId() != null, StudentScoreEntity::getClassId, studentScoreEntity.getClassId());

        if( pageNo == 0 )
        {
            pageNo = 1;
        }
        if( pageSize == 0 ){
            pageSize = 10;
        }

        IPage<StudentScoreEntity> page = Page.of(pageNo, pageSize);

        return baseMapper.selectPage(page, query);
    }
    @Override
    public List<StudentScoreEntity> getScoreByUserId(Long userId) {
        LambdaQueryWrapper<StudentScoreEntity> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(StudentScoreEntity::getUserId, userId);
        return baseMapper.selectList(queryWrapper);
    }

    @Override
    public List<StudentScoreEntity> getScoreByClassId(Long classId) {
        LambdaQueryWrapper<StudentScoreEntity> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(StudentScoreEntity::getClassId, classId);
        return baseMapper.selectList(queryWrapper);
    }

    @Override
    public Double getAverageScoreByPaperIdAndUserId(Long paperId, Long userId) {
        return studentScoreDao.selectAverageScoreByPaperIdAndUserId(paperId, userId);
    }

    @Override
    public Double getAverageScoreByPaperIdAndClassIdAndExamTime(Long paperId, Long classId, LocalDateTime examTime) {
        return studentScoreDao.selectAverageScoreByPaperIdAndClassIdAndExamTime(paperId, classId, examTime);
    }
}
